<template>
  <div class="regForm">
    <el-form
      :model="regForm"
      :rules="rules"
      ref="regForm"
      class="demo-ruleForm"
    >
      <el-form-item prop="username">
        <el-input
          clearable
          v-model.number="regForm.username"
          placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="userEmail">
        <el-input
          clearable
          v-model="regForm.userEmail"
          placeholder="邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          clearable
          :type="flag ? 'text' : 'password'"
          v-model="regForm.password"
          autocomplete="off"
          placeholder="密码"
        >
          <i
            slot="suffix"
            :class="[flag ? 'el-icon-view' : 'el-icon-remove']"
            style="position: relative; right: 7px; top: 1px; font-size: 15px"
            @click="flag = !flag"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input
          clearable
          type="password"
          v-model="regForm.checkPass"
          autocomplete="off"
          placeholder="确认密码"
        >
        </el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "regForm",
  data() {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      } else if (this.nameErr) {
        return callback(new Error("用户名已存在"));
      }
      callback();
    };
    var checkEmail = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("邮箱不能为空"));
      } else if (this.emailErr) {
        return callback(new Error("邮箱已存在"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.checkPass != "") {
          this.$refs.regForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请再次输入密码"));
      } else if (value != this.regForm.password) {
        console.log(value);
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      nameErr: false,
      emailErr: false,
      pwdErr: false,
      //密码小眼睛切换状态
      flag: false,
      regForm: {
        username: "",
        userEmail: "",
        password: "",
        checkPass: "",
      },
      rules: {
        username: [{ validator: checkName, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        userEmail: [{ validator: checkEmail, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios({
            url: "/user/regist",
            method: "POST",
            data: JSON.stringify(this.regForm),
          })
            .then((res) => {
              console.log(res.data);
              if (res.data.status == 200) {
                this.$message.success(res.data.msg);
                setTimeout(() => {
                  this.$router.go(0);
                }, 2000);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(() => {
              this.$message.error("请求服务器错误");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    /* resetForm(formName) {
      this.$refs[formName].resetFields();
    }, */
  },
};
</script>

<style scoped>
.regForm >>> .el-input input {
  height: 30px;
  border-radius: 10px;
}
</style>
